package backtracking_finished;

import java.util.ArrayList;
import java.util.List;

public class T77 {

    List<List<Integer>> ret = new ArrayList<>();
    List<Integer> list = new ArrayList<>();
    public List<List<Integer>> combine(int n, int k) {
        backtracking(n, k, 1);
        return ret;
    }

    /**
     *
     * @param n 数组是1~n
     * @param k 集合包含k个数
     * @param startIndex 从startIndex下标开始遍历
     */
    public void backtracking (int n, int k, int startIndex) {
        //假如当前集合个数已经符合了要求，就停止回溯
        if(list.size() == k) {
            ret.add(new ArrayList<>(list));
            return;
        }

        for (int i = startIndex; i <= n; i++) {
            list.add(i);
            backtracking(n, k, i+1);
            list.remove(list.size() - 1);
        }

    }

    public static void main(String[] args) {
        T77 cl = new T77();
        cl.combine(4,2);
        System.out.println(cl.ret.toString());
    }

}
